﻿namespace EyeSoft.Data.EntityFramework
{
	using System.Data.Common;
	using System.Data.Entity;
	using System.Data.Entity.Infrastructure;

	using EyeSoft.Domain;
	using EyeSoft.Domain.Transactional;

	public class EntityFrameworkTransactionalCollection :
		EntityFrameworkReadonlyTransactionalCollection,
		ITransactionalCollection
	{
		public EntityFrameworkTransactionalCollection(string nameOrConnectionString, DbCompiledModel model)
			: base(nameOrConnectionString, model)
		{
		}

		public EntityFrameworkTransactionalCollection(DbConnection connection, DbCompiledModel model)
			: base(connection, model)
		{
		}

		public EntityFrameworkTransactionalCollection(DbContext dbContext)
			: base(dbContext)
		{
		}

		public void Add<T>(T entity) where T : class, IAggregate
		{
			context.Set<T>().Add(entity);
		}

		public void Delete<T>(T entity) where T : class, IAggregate
		{
			context.Set<T>().Remove(entity);
		}

		public void Update<T>(T entity) where T : class, IAggregate
		{
			context.Entry(entity);
		}

		public void Commit()
		{
			context.SaveChanges();
		}
	}
}